import lombok.SneakyThrows;

import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @author 乐心湖
 * @version 1.0
 * @date 2021/9/8 13:15
 */

public class ClientRecv implements Runnable {
    private ObjectInputStream ois;
    private boolean first = true;
    //是否保持连接状态
    private boolean isConnected = true;

    @SneakyThrows
    @Override
    public void run() {
        while (true) {
            if (first || MyClientSocket.isOisUpdated()) {
                //更新输入流
                ois = new ObjectInputStream(MyClientSocket.getSocket().getInputStream());
                first = false;
                MyClientSocket.setOisUpdated(false);
            }
            if (!MyClientSocket.isOisUpdated()) {
                try {
                    System.out.println(ois.readObject());
                } catch (IOException | ClassNotFoundException e) {
                    //算是掉线了吧。。。不知道
                    System.out.println("服务器掉线了，正在重连中");
                    MyClientSocket.setSocket(ConnectedUtils.getConnection());
                    this.first = true;
                    System.out.println("重新连接上了，请按下回车键确定");
                }
            }
        }
    }
}
